//
//  oldEvaluateViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/11/11.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldEvaluateViewController.h"
#import "UIImageView+oldImageView.h"

@interface oldEvaluateViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn_commit;
@property (weak, nonatomic) IBOutlet UIButton *star1;
@property (weak, nonatomic) IBOutlet UIButton *star2;
@property (weak, nonatomic) IBOutlet UIButton *star3;
@property (weak, nonatomic) IBOutlet UIButton *star4;
@property (weak, nonatomic) IBOutlet UIButton *star5;
@property (weak, nonatomic) IBOutlet UIView *star_view;
@property (weak, nonatomic) IBOutlet UIButton *btn_anonymous;
@property (weak, nonatomic) IBOutlet UITextView *text_view;
@property (weak, nonatomic) IBOutlet UILabel *label_placeholder;
@property (weak, nonatomic) IBOutlet UIImageView *img_sever;
@property (weak, nonatomic) IBOutlet UILabel *label_severName;
@property (weak, nonatomic) IBOutlet UILabel *label_severAge;
@property (weak, nonatomic) IBOutlet UILabel *label_orderNumber;
@property (weak, nonatomic) IBOutlet UIButton *litterStar1;
@property (weak, nonatomic) IBOutlet UIButton *litterStar2;
@property (weak, nonatomic) IBOutlet UIButton *litterStar3;
@property (weak, nonatomic) IBOutlet UIButton *litterStar4;
@property (weak, nonatomic) IBOutlet UIButton *litterStar5;
@property (nonatomic, copy)NSString *point;
@property (nonatomic, assign)BOOL anonymous;
@end

#define STAR_ON [UIImage imageNamed:@"评价星星-绿"]
#define STAR_OFF [UIImage imageNamed:@"评价星星-白"]

@implementation oldEvaluateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"评价";
    [self initRightBtn];
    [self initTopView];
    [self initTextView];
    [self initAnonymous];
    [self initStar];
    [self.btn_commit addTarget:self action:@selector(commit:) forControlEvents:UIControlEventTouchDown];
    _btn_commit.layer.cornerRadius = 6.f;
}

#pragma mark - rightBtn
- (void)initRightBtn
{
    UIButton *rightbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    
    //[leftbutton setBackgroundColor:[UIColor blackColor]];
    
    [rightbutton setTitle:@"完成" forState:UIControlStateNormal];
    
    [rightbutton addTarget:self action:@selector(rightbtnTapped:) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:rightbutton];
    
    self.navigationItem.rightBarButtonItem = rightitem;
}

- (void)rightbtnTapped:(id)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - initTextView
- (void)initTextView
{
    _text_view.delegate = self;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (_label_placeholder) {
        if ([textView.text length] == 0) {
            [_label_placeholder setHidden:NO];
        }
        else
        {
            [_label_placeholder setHidden:YES];
        }
    }
}

#pragma mark - topView

- (void)initTopView
{
    _img_sever.layer.cornerRadius = 30.f;
    [_img_sever showHeadImg:_orderInfo.serverInfo.avatar];
    [self starsNumber:_orderInfo.serverInfo.addition.userRate];
    _label_severAge.text = [NSString stringWithFormat:@"%@岁", _orderInfo.serverInfo.age];
    _label_orderNumber.text = [NSString stringWithFormat:@"已服务：%@单", _orderInfo.serverInfo.addition.orderCount];
}

- (void)starsNumber:(NSString *)rate
{
    NSInteger star = (NSInteger)([rate floatValue] * 100)/20 + 1;
    
    switch (star) {
        case 0:
            [_litterStar1 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar2 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar3 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar4 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar5 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            break;
        case 1:
            [_litterStar1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar2 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar3 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar4 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar5 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            break;
        case 2:
            [_litterStar1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar3 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar4 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar5 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            break;
        case 3:
            [_litterStar1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar3 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar4 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            [_litterStar5 setImage:[UIImage imageNamed:@"我的-评价星星空"] forState:UIControlStateNormal];
            break;
        case 4:
            [_litterStar1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar3 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar4 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar5 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            break;
        default:
            [_litterStar1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar3 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar4 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_litterStar5 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            break;
    }
}

#pragma mark - initAnonymous
- (void)initAnonymous
{
    _anonymous = YES;
    [_btn_anonymous addTarget:self action:@selector(btn_anonymous_tapped) forControlEvents:UIControlEventTouchDown];
}

- (void)btn_anonymous_tapped
{
    _anonymous = !_anonymous;
    if (_anonymous) {
        [_btn_anonymous setImage:[UIImage imageNamed:@"匿名-是"] forState:UIControlStateNormal];
    }
    else
    {
        [_btn_anonymous setImage:[UIImage imageNamed:@"匿名-否"] forState:UIControlStateNormal];
    }
}

#pragma mark - other

- (void)commit:(id)sender
{
    NSString *anonymousStr = _anonymous ? @"1" : @"0";
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:_orderInfo.oid, @"oid", _point, @"rate", anonymousStr, @"anonymous", nil];
    
    if (_text_view.text) {
        [params setObject:_text_view.text forKey:@"content"];
    }
    
    [oldHub waiting:@"提交中.."];
    
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_AddEstimate params:params success:^(id data) {
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:^{
            [oldHub warningUser:@"评价成功" time:1.f];
        }];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)initStar
{
    _point = @"5";
    
    _star1.tag = 1;
    _star2.tag = 2;
    _star3.tag = 3;
    _star4.tag = 4;
    _star5.tag = 5;
    
    [_star1 addTarget:self action:@selector(setStar:) forControlEvents:UIControlEventTouchDown];
    [_star2 addTarget:self action:@selector(setStar:) forControlEvents:UIControlEventTouchDown];
    [_star3 addTarget:self action:@selector(setStar:) forControlEvents:UIControlEventTouchDown];
    [_star4 addTarget:self action:@selector(setStar:) forControlEvents:UIControlEventTouchDown];
    [_star5 addTarget:self action:@selector(setStar:) forControlEvents:UIControlEventTouchDown];
}

- (void)setStar:(UIButton *)btn
{
    [self setStars:btn.tag];
}

- (void)setStars:(NSInteger)index
{
    switch (index) {
        case 1:
        {
            [_star1 setImage:STAR_ON forState:UIControlStateNormal];
            [_star2 setImage:STAR_OFF forState:UIControlStateNormal];
            [_star3 setImage:STAR_OFF forState:UIControlStateNormal];
            [_star4 setImage:STAR_OFF forState:UIControlStateNormal];
            [_star5 setImage:STAR_OFF forState:UIControlStateNormal];
        }
            break;
        case 2:
        {
            [_star1 setImage:STAR_ON forState:UIControlStateNormal];
            [_star2 setImage:STAR_ON forState:UIControlStateNormal];
            [_star3 setImage:STAR_OFF forState:UIControlStateNormal];
            [_star4 setImage:STAR_OFF forState:UIControlStateNormal];
            [_star5 setImage:STAR_OFF forState:UIControlStateNormal];
        }
            break;
        case 3:
        {
            [_star1 setImage:STAR_ON forState:UIControlStateNormal];
            [_star2 setImage:STAR_ON forState:UIControlStateNormal];
            [_star3 setImage:STAR_ON forState:UIControlStateNormal];
            [_star4 setImage:STAR_OFF forState:UIControlStateNormal];
            [_star5 setImage:STAR_OFF forState:UIControlStateNormal];
        }
            break;
        case 4:
        {
            [_star1 setImage:STAR_ON forState:UIControlStateNormal];
            [_star2 setImage:STAR_ON forState:UIControlStateNormal];
            [_star3 setImage:STAR_ON forState:UIControlStateNormal];
            [_star4 setImage:STAR_ON forState:UIControlStateNormal];
            [_star5 setImage:STAR_OFF forState:UIControlStateNormal];
        }
            break;
        case 5:
        {
            [_star1 setImage:STAR_ON forState:UIControlStateNormal];
            [_star2 setImage:STAR_ON forState:UIControlStateNormal];
            [_star3 setImage:STAR_ON forState:UIControlStateNormal];
            [_star4 setImage:STAR_ON forState:UIControlStateNormal];
            [_star5 setImage:STAR_ON forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
    
    _point = [NSString stringWithFormat:@"%ld", (long)index];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint lastLocation = [touch locationInView:self.star_view];
    
    NSInteger index = (lastLocation.x)/70;
    
    switch (index) {
        case 0:
            [self setStar:_star1];
            break;
        case 1:
            [self setStar:_star2];
            break;
        case 2:
            [self setStar:_star3];
            break;
        case 3:
            [self setStar:_star4];
            break;
        case 4:
            [self setStar:_star5];
            break;
        default:
            break;
    }
    
}

@end
